import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {path:'/',redirect:'/index/1'},
  {path:'/order',component:()=>import('@/views/Order.vue')},
  {
    path: '/index/:type',
    component: ()=>import('@/views/Index.vue'),
  },
  {
    path: '/index/:type/:id',
    component: ()=>import('@/views/Detail.vue'),
  },
  {
    path: '/coupon',
    component: ()=>import('@/views/Coupon.vue'),
  },
  {
    path: '/user_order',
    component: ()=>import('@/views/UserOrder.vue'),
  },
  {
    path: '/profile',
    component: ()=>import('@/views/Profile.vue'),
  },
  {
    path: '/load',
    component: ()=>import('@/views/Loading.vue'),
  },
  {
    path:'/admin',
    component:()=>import('@/views/Admin.vue'),
    redirect:'/admin/user',
    children:[
      {path:'user',component:()=>import('@/views/User.vue')},
      {path:'good',component:()=>import('@/views/Good.vue')},
      {path:'pwd',component:()=>import('@/views/Pwd.vue')},
      {path:'order',component:()=>import('@/views/AdminOrder.vue')},
      {path:'type',component:()=>import('@/views/AdminType.vue')},
      {path:'coupon',component:()=>import('@/views/AdminCoupon.vue')},
      {path:'sessions',component:()=>import('@/views/SessionManagement.vue')},
    ]
  },
  {
    path:'/login',
    component:()=>import('@/views/Login.vue')
  }
 
]

const router = new VueRouter({
  mode: 'history',
  routes
})
router.beforeEach((to,from,next)=>{
  const user = localStorage.getItem('user')
  if(to.path.includes("admin")){
    if(user){
      user.role_id==1?next():next("/")
      
    }else{
      next("/login")
    }
  }else{
    next()
  }
  
})

export default router
